package com.pay.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayUserAgreementPageSignRequest;
import com.alipay.api.request.AlipayUserAgreementQueryRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayUserAgreementPageSignResponse;
import com.alipay.api.response.AlipayUserAgreementQueryResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝签约信息
 *
 * @author chen
 */
public class AliPaySignService {

    private AliConfig aliConfig;

    public AliPaySignService(AliConfig aliConfig) {
        this.aliConfig = aliConfig;
    }


    /**
     * 支付宝个人协议页面签约接口
     *
     * @param personal_product_code 个人签约产品码，商户和支付宝签约时确定，商户可咨询技术支持。
     * @param external_agreement_no 商户签约号，代扣协议中标示用户的唯一签约号（确保在商户系统中唯一）。
     * @param sign_scene            协议签约场景，商户和支付宝签约时确定，商户可咨询技术支持。当传入商户签约号external_agreement_no时，场景不能为默认值DEFAULT|DEFAULT。
     * @return
     * @throws AlipayApiException
     */
    public Map<String, Object> agreementPageSign(String notifyUrl, String personal_product_code, String external_agreement_no, String sign_scene) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliConfig.appid(), aliConfig.private_key(), "json", "GBK", aliConfig.alipay_public_key(), "RSA2");
        AlipayUserAgreementPageSignRequest request = new AlipayUserAgreementPageSignRequest();
        //设置回调地址
        request.setNotifyUrl(notifyUrl);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("personal_product_code", personal_product_code);
        paramMap.put("sign_scene", sign_scene);
        //商户签约号，在商户系统中唯一
        paramMap.put("external_agreement_no", external_agreement_no);
        String param = JSONObject.toJSONString(paramMap);
        request.setBizContent(param);
        AlipayUserAgreementPageSignResponse response = alipayClient.pageExecute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return null;
    }

    /**
     * 签约情况查询
     *
     * @param personal_product_code 个人签约产品码，商户和支付宝签约时确定，商户可咨询技术支持。
     * @param external_agreement_no 商户签约号，代扣协议中标示用户的唯一签约号（确保在商户系统中唯一）。
     * @param sign_scene            协议签约场景，商户和支付宝签约时确定，商户可咨询技术支持。当传入商户签约号external_agreement_no时，场景不能为默认值DEFAULT|DEFAULT。
     * @return
     * @throws AlipayApiException
     */
    public Map<String, Object> agreementQuery(String personal_product_code, String sign_scene, String external_agreement_no) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliConfig.appid(), aliConfig.private_key(), "json", "GBK", aliConfig.alipay_public_key(), "RSA2");
        AlipayUserAgreementQueryRequest request = new AlipayUserAgreementQueryRequest();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("personal_product_code", personal_product_code);
        paramMap.put("sign_scene", sign_scene);
        //商户签约号，在商户系统中唯一
        paramMap.put("external_agreement_no", external_agreement_no);
        String param = JSONObject.toJSONString(paramMap);
        request.setBizContent(param);
        AlipayUserAgreementQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return null;
    }

    /**
     * 代扣支付
     *
     * @param notifyUrl    回调地址
     * @param out_trade_no 订单编号
     * @param total_amount 总金额
     * @param subject      订单描述
     * @param product_code 产品码
     * @param agreement_no 订单金额
     * @return
     * @throws AlipayApiException
     */
    public Map<String, Object> withholdingPayment(String notifyUrl, String out_trade_no, String total_amount, String subject, String product_code, String agreement_no) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliConfig.appid(), aliConfig.private_key(), "json", "GBK", aliConfig.alipay_public_key(), "RSA2");
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        //回调地址
        request.setNotifyUrl(notifyUrl);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("out_trade_no", out_trade_no);
        paramMap.put("total_amount", total_amount);
        paramMap.put("subject", subject);
        //协议相关参数
        Map<String, Object> agreement_params = new HashMap<>();
        agreement_params.put("agreement_no", agreement_no);
        paramMap.put("agreement_params", JSONObject.toJSONString(agreement_params));
        paramMap.put("product_code", product_code);
        request.setBizContent(JSONObject.toJSONString(paramMap));
        AlipayTradePayResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return null;
    }

}

